
package com.yix.common.base;

import com.alibaba.fastjson.annotation.JSONField;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;

/**
 * @author wdx
 * @version 1.0
 * @Description TODO
 * @date 2021/1/19 14:03
 */
@MappedSuperclass
@Data
public class BaseUUIDEntity implements Serializable {
    @Id
    @Column(name = "id",
            length = 32,
            nullable = false,
            columnDefinition = "varchar(32) COMMENT '主键id'")
    @GenericGenerator(
            name = "uuid2", // 生成器名称
            strategy = "org.hibernate.id.UUIDHexGenerator" // UUID（32位）生成类
    )
    @NotNull(groups = {Update.class})
    @GeneratedValue(generator = "uuid2") // 指定生成器名称（对应@GenericGenerator中的name属性）
    @ApiModelProperty(value = "主键ID（新增时，ID可以为空）")
    protected String id;

    public @interface Update {}

    @Column(name = "updater_id",
            length = 32,
            columnDefinition = "varchar(32) COMMENT '更新人ID'")
    @ApiModelProperty(hidden = true) // 在swagger-ui的Parameters中，隐藏此字段
    @JSONField(serialize = false)
    protected String updaterId;

    @Column(name = "update_time",
            columnDefinition = "datetime COMMENT '更新时间'")
    @UpdateTimestamp
    @ApiModelProperty(hidden = true) // 在swagger-ui的Parameters中，隐藏此字段
    protected Timestamp updateTime;

    @Column(name = "creater_id",
            length = 32,
            updatable = false, // update时，不更新creater_id字段，此字段只参与insert
            columnDefinition = "varchar(32) COMMENT '创建人ID'")
    @ApiModelProperty(hidden = true) // 在swagger-ui的Parameters中，隐藏此字段
    protected String createrId;

    @Column(name = "create_time",
            updatable = false, // update时，不更新create_time字段，此字段只在insert时生成一次
            columnDefinition = "datetime COMMENT '创建时间'") // 不同数据库需要定义不同的语句
    @CreationTimestamp
    @ApiModelProperty(hidden = true) // 在swagger-ui的Parameters中，隐藏此字段
    protected Timestamp createTime;
}
